package com.yy.system.service;

import java.util.List;
import org.springframework.stereotype.Service;
import com.mes.entity.User;
import com.yy.base.common.service.BaseService;
import com.yy.system.entity.Role;
import com.yy.system.entity.UserRole;

@Service("userService")
@SuppressWarnings("unchecked")
public class UserService extends BaseService {
	/*
	 * 根据ID查询用户
	 */
	public User getUserInfoById(String id){
		return dao.get(User.class, id);
	}
	/*
	 * 检查用户名
	 */
	public boolean checkUser(String userCode){
		User user = (User)dao.findUnique("from User where userCode=?", userCode);
		if(user != null){
			return true;
		}
		return false;
	}
	/*
	 * 查询所有用户
	 */
	public List<Role> getAllRole(){
		return dao.find("from Role where isdelete=0");
	} 
	
	/**
	 * 根据ID查询角色
	 */
	public List getRoleByUserId(String id){
		List role = dao.query("select * from t_role u where isDelete=0 and u.id in ("+
				"select r.roleId from t_userrole r where r.userId = ?)",id);
		return role;
	}
	/**
	 * 查询未设置的角色 
	 */
	public List getRoleNotInUser(String id){
		List role = dao.splitQuery("select * from t_role u where isDelete=0 and u.id not in ("+
				"select r.roleId from t_userrole r where r.userId = ?)",id);
		return role;
	}
	/**
	 * 移除角色
	 */
	public void delectUser(UserRole userRole){
		 dao.executeSql("delete from t_userrole where userId=? and roleId=?", userRole.getUserId(), userRole.getRoleId());
	}
}
